import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/elec_price_title_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';

import 'effective_data_widget.dart';

class EffectiveDateWidgets extends StatelessWidget {
  final List<dynamic> timeDateList;
  final Function() addCallBack;
  final Function(int index) deleteCallBack;
  final bool isEdit;
  final bool isRequired;
  final Function() dateCallBack;
  const EffectiveDateWidgets(
      this.timeDateList, this.addCallBack, this.deleteCallBack,this.dateCallBack,
      {this.isEdit = true,this.isRequired = false,Key? key})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
          clipBehavior: Clip.hardEdge,
          margin: const EdgeInsets.only(left: 16,right: 16),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8),
            color: Colors.white,
          ),
        child: Column(
          children: [
            ElecPriceTitleWidget(ResKey.siteSettingEffectiveDate.tr),
            ListView.builder(
              padding: EdgeInsets.zero,
              shrinkWrap: true,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (BuildContext context, int index) {
               var effectiveTime = timeDateList[index]['effectiveTime'].toString().replaceAll('-', '/');
               var expirationTime = timeDateList[index]['expirationTime'].toString().replaceAll('-', '/');
                return  EffectiveDataWidget(
                  context,
                  '${ResKey.siteSettingDate.tr}${index + 1}',
                  effectiveTime,
                  expirationTime,
                      (startDateTime, endDateTime) {
                    timeDateList[index] = {
                      'effectiveTime': startDateTime,
                      'expirationTime': endDateTime,
                    };
                    // controller.update();
                    dateCallBack();
                  },
                      () {
                    deleteCallBack(index);
                  },
                  isDelete: true,
                  isEdit: isEdit,
                  value: '$effectiveTime-$expirationTime',
                  isRequired: isRequired,
                );
              }
                 ,
              itemCount: timeDateList.length,
            ),

            Container(
              height: 0.5,
              color: Colour.dividerColor,
              margin: EdgeInsets.only(left: 16.w, right: 16.w),
            ),
            Visibility(
                visible: isEdit,
                child: GestureDetector(
                behavior: HitTestBehavior.opaque,
                onTap: () {
                  addCallBack();
                },
                child: SizedBox(
                  height: 49,
                  child: Row(
                    children: [
                      Container(
                        padding: EdgeInsets.only(left: 16, right: 16),
                        child: Image.asset(
                          'assets/images/site_elec_price_add.png',
                          height: 18,
                          width: 18,
                        ),
                      ),
                      Text(
                        ResKey.siteSettingElectricityAddDate.tr,
                        style: ytTextStyle(
                            fontWeight: FontWeight.normal,
                            color: Colour.primaryColor,
                            fontSize: 14),
                      ),
                    ],
                  ),
                )))
          ],
        ));
  }

  
}
